package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.entity.member.GrowConfigEntity;
import com.dragon.modules.yuegou.enums.code.MemberCode;
import com.dragon.modules.yuegou.mapper.member.GrowConfigMapper;
import com.dragon.modules.yuegou.model.member.GrowConfig;
import com.dragon.modules.yuegou.query.member.GrowConfigQuery;
import com.dragon.modules.yuegou.service.member.GrowConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 成长值管理表 服务实现
 *
 * @author pbuilder generated
 * @version 2.0
 * @date 2021-12-15 17:09:47
 */
@Service("memGrowConfigService")
public class GrowConfigServiceImpl
        extends ServiceImpl<GrowConfigMapper, GrowConfigEntity>
        implements GrowConfigService {

    public List<GrowConfig> growConfigList(GrowConfigQuery query) {
        return list(query.toQueryWrapper()).stream().map(GrowConfig::new).collect(Collectors.toList());
    }

    public GrowConfig saveGrowConfig(GrowConfig growConfig) {
        QueryWrapper<GrowConfigEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(GrowConfigEntity.COL_ACTION_CODE, growConfig.getActionCode());
        queryWrapper.eq(GrowConfigEntity.COL_BUSD_ID, growConfig.getBusdId());
        if (StringUtils.isNotEmpty(growConfig.getId())) {
            queryWrapper.notIn(GrowConfigEntity.COL_ID, growConfig.getId());
        }
        Long count = this.baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new DragonException(MemberCode.MEMBER_GROW_CONFIG);
        }
        this.save(growConfig);
        return growConfig;
    }
}